import {
    WORK_BERTH,
    TODAY_ORDER,
    TODAY_TALLYING,
    TEMPER_DATA,
    WORLD_ORDER,
    CHINA_ORDER,
    CHINA_LINE,
    ORDER_PUNCTUALITY,
    ORDER_RATE,
    PRODUCT_DAILY,
    PRODUCT_LOW,
    SCHEDULING,
    SCHEDULING_STAT,
    ORDER_LIST,
    FILTER_TABLE,
    INVENTORY,
    SIGNAL_SPY,
    SIGNAL_SPY_WEEK,
    SIGNAL_SPY_ALL,
    COMPLETE_SPY,
    COMPLETE_SPY_WEEK,
    COMPLETE_SPY_ALL,
    TOTAL_SPY,
    TOTAL_SPY_ALL,
    DEVICE_LIST,
    CHECK_DAY,
    CHECK_MONTH,
    EXCEPTION_LIST,
    EXCEPTION_CYCLE,
    EXCEPTION_CLOSE,
    DELIVERY_COUNT,
    SIGNAL_SPY_TIME,
    COMPLETE_SPY_TIME,
    PRODUCTION_COUNT,
    REFLOW,
    PRESS,
    SPI,
    CLIP,
    SIGNAL_SPY_TIME_ALL,
    COMPLETE_SPY_TIME_ALL,
    EXCEPTION_STAT,
    AOI,
    WELDER,
    WAVE,
    AXI
} from '../../action/map.action.js'
import { List, Map ,fromJS} from 'immutable'
const defaultData = []

/*今日订单看板*/
const todayOrderInit = List([])
export function todayOrder (state = todayOrderInit, action) {
    switch (action.type) {
        case TODAY_ORDER:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*今日订单看板*/
const todayTallyingInit = Map({})
export function todayTallying (state = todayTallyingInit, action) {
    switch (action.type) {
        case TODAY_TALLYING:
            return state.merge(action.data)
        default:
            return state
    }
}

/*温室度数据*/
const temperInit = List([])
export function temperData (state = temperInit, action) {
    switch (action.type) {
        case TEMPER_DATA:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*全球发货*/
const worldOrderInit = List([])
export function worldOrder (state = worldOrderInit, action) {
    switch (action.type) {
        case WORLD_ORDER:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*国内发货*/
const chinaOrderInit = List([])
export function chinaOrder (state = chinaOrderInit, action) {
    switch (action.type) {
        case CHINA_ORDER:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*国内发货线图*/
const chinaLineInit = List([])
export function chinaLine (state = chinaLineInit, action) {
    switch (action.type) {
        case CHINA_LINE:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*交付率*/
const orderRateInit = List([])
export function orderRate (state = orderRateInit, action) {
    switch (action.type) {
        case ORDER_RATE:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*正点率*/
const orderPunctualityInit = List([])
export function orderPunctuality (state = orderPunctualityInit, action) {
    switch (action.type) {
        case ORDER_PUNCTUALITY:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*每日库存需求满足率*/
const productDailyInit = List([])
export function productDaily (state = productDailyInit, action) {
    switch (action.type) {
        case PRODUCT_DAILY:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*最低库存需求满足率*/
const productLowInit = List([])
export function productLow (state = productLowInit, action) {
    switch (action.type) {
        case PRODUCT_LOW:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*排产计划*/
const schedulingInit = List([])
export function scheduling (state = schedulingInit, action) {
    switch (action.type) {
        case SCHEDULING:
            action.data.forEach((d, i) => d.key = i+1)
            return state.clear().merge(action.data)
        default:
            return state
    }
}

/*排产计划完成占比*/
const schedulingStatInit = Map({})
export function schedulingStat (state = schedulingStatInit, action) {
    switch (action.type) {
        case SCHEDULING_STAT:
            return state.merge(action.data)
        default:
            return state
    }
}

/*订单*/
const orderListInit = List([])
let order  = []
export function orderList (state = orderListInit, action) {
    switch (action.type) {
        case ORDER_LIST:
            order = action.data
            return state.clear().merge(action.data)
        case FILTER_TABLE:
            return fromJS(order.filter(d=>d.projectName.indexOf(action.param.keyword)!==-1))
        default:
            return state
    }
}

/*库存概况*/
const inventoryInit = List([])
export function inventory (state = inventoryInit, action) {
    switch (action.type) {
        case INVENTORY:
            return state.clear().merge(action.data)
        default:
            return state
    }
}

const spyData = {
    signal: {},
    signalTime: {},
    signalTimeAll: {},
    signalWeek: {},
    complete: {},
    completeTime: {},
    completeTimeAll: {},
    completeWeek: {},
    total: {},
    signalAll: {},
    completeAll: {},
    totalAll: {},
}
/*合格率趋势图*/
export function fpy (state = spyData, action) {
    switch (action.type) {
        case SIGNAL_SPY:
            return Object.assign({}, state, {signal: action.data})
        case SIGNAL_SPY_WEEK:
            return Object.assign({}, state, {signalWeek: action.data})
        case COMPLETE_SPY:
            return Object.assign({}, state, {complete: action.data})
        case COMPLETE_SPY_WEEK:
            return Object.assign({}, state, {completeWeek: action.data})
        case TOTAL_SPY:
            return Object.assign({}, state, {total: action.data})
        case SIGNAL_SPY_ALL:
            return Object.assign({}, state, {signalAll: action.data})
        case COMPLETE_SPY_ALL:
            return Object.assign({}, state, {completeAll: action.data})
        case TOTAL_SPY_ALL:
            return Object.assign({}, state, {totalAll: action.data})
        case SIGNAL_SPY_TIME:
            return Object.assign({}, state, {signalTime: action.data})
        case COMPLETE_SPY_TIME:
            return Object.assign({}, state, {completeTime: action.data})
        case SIGNAL_SPY_TIME_ALL:
            // return Object.assign({}, state, {signalTimeAll: action.data})
            return Object.assign({}, state, {signalTimeAll: {'全部': {values: [{value: 100}]}}})
        case COMPLETE_SPY_TIME_ALL:
            // return Object.assign({}, state, {completeTimeAll: action.data})
            return Object.assign({}, state, {completeTimeAll: {'全部': {values: [{value: 100}]}}})
        default:
            return state
    }
}
const deviceData = {
    'SMT一线': [],
    'SMT二线': []
}

const deviceDataInit = Map(deviceData)
/*设备*/
export function deviceList (state = deviceDataInit, action) {
    switch (action.type) {
        case DEVICE_LIST:
            return state.merge(action.data)
        default:
            return state
    }
}

/*检验合格率日趋势统计图*/
const checkDayInit = Map({})
export function checkDay (state = checkDayInit, action) {
    switch (action.type) {
        case CHECK_DAY:
            return state.merge(action.data)
        default:
            return state
    }
}

/*检验合格率月度趋势统计图*/
const checkMonthInit = Map({})
export function checkMonth (state = checkMonthInit, action) {
    switch (action.type) {
        case CHECK_MONTH:
            return state.merge(action.data)
        default:
            return state
    }
}

/*质量预警*/
const exceptionListInit = List([])
export function exceptionList (state = exceptionListInit, action) {
    switch (action.type) {
        case EXCEPTION_LIST:
            return state.clear().merge(action.data)
        default:
            return state
    }
}
/*已关闭问题处理周期分布*/
const exceptionCycleInit = Map({})
export function exceptionCycle (state = exceptionCycleInit, action) {
    switch (action.type) {
        case EXCEPTION_CYCLE:
            return state.merge(action.data)
        default:
            return state
    }
}

/*问题处理状态占比*/
const exceptionStatInit = Map({})
export function exceptionStat (state = exceptionStatInit, action) {
    switch (action.type) {
        case EXCEPTION_STAT:
            return state.merge(action.data)
        default:
            return state
    }
}

/*问题单及时关闭率*/
const exceptionCloseInit = Map({})
export function exceptionClose (state = exceptionCloseInit, action) {
    switch (action.type) {
        case EXCEPTION_CLOSE:
            return state.merge(action.data)
        default:
            return state
    }
}

/*发货统计*/
const deliveryCountInit = Map({DAY: '', MONTH: ''})
export function deliveryCount (state = deliveryCountInit, action) {
    switch (action.type) {
        case DELIVERY_COUNT:
            return state.merge(action.data)
        default:
            return state
    }
}

/*产出统计*/
const productionCountInit = Map({DAY: '', MONTH: ''})
export function productionCount (state = productionCountInit, action) {
    switch (action.type) {
        case PRODUCTION_COUNT:
            return state.merge(action.data)
        default:
            return state
    }
}
const deviceMetricsData = Map({
    reflow: Map({}),
    press: Map({}),
    spi: Map({}),
    clip: Map({}),
    aoi: Map({}),
    welder: Map({}),
    wave: Map({}),
    axi: Map({}),
})
/*设备详情*/
export function deviceMetrics (state = deviceMetricsData, action) {
    switch (action.type) {
        case REFLOW:
            return state.update('reflow', data => data.merge(action.data))
        case PRESS:
            return state.update('press', data => data.merge(action.data))
        case SPI:
            return state.update('spi', data => data.merge(action.data))
        case CLIP:
            return state.update('clip', data => data.merge(action.data))
        case AOI:
            return state.update('aoi', data => data.merge(action.data))
        case WELDER:
            return state.update('welder', data => data.merge(action.data))
        case WAVE:
            return state.update('wave', data => data.merge(action.data))
        case AXI:
            return state.update('axi', data => data.merge(action.data))
        default:
            return state
    }
}


